package com.neuedu.mvc.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet( "/login")
public class LoginController  extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();
        //接收用户名密码，
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        boolean success = false;
        //跟数据库匹配， admin 123456
        if("admin".equals(username)   && "123456".equals(password)){
            session.setAttribute("username",username);
            session.setAttribute("status",true);
            success = true;
        }



        String result = "{'status':"+success+"}";
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(result);
        writer.flush();
        writer.close();


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
